#include "MainHeader.h"

int GetScroll(){

  fseek(GBA.ROM, scrollpnt,SEEK_SET);
  fread(&scroll.Room,1,1,GBA.ROM);
  
  fread(&scroll.Number,1,1,GBA.ROM);
  for(int i = 0; i<=scroll.Number+1;i++){
  fread(&scroll.Scrolls[i]. xStart,1,1,GBA.ROM);
  fread(&scroll.Scrolls[i]. xEnd,1,1,GBA.ROM);
  fread(&scroll.Scrolls[i]. yStart,1,1,GBA.ROM);
  fread(&scroll.Scrolls[i]. yEnd,1,1,GBA.ROM);
  }

return 0;
}//End Function

int DrawScrolls(int ScrollToDraw){

RECT blah; 

sctype thisscroll;

if(scroll.Room != 0xFF) {


	thisscroll=scroll.Scrolls[ScrollToDraw];
			

			
			if ((thisscroll.xStart != 0xFF) && (thisscroll.yEnd != 0xFF)){
   

				  
				
				  
			
				
     
                     HBRUSH curbrush = CreateSolidBrush(
										RGB( 
										255-
										((ScrollToDraw+3)<<2)|128, 
										255-((ScrollToDraw+2)<<4)|128, 
										255-((ScrollToDraw+1)<<8)|128
										)
										);
																
				
						for(int d=0;d<2;d++){
								blah.left = (thisscroll.xStart) * 16+d;
								blah.top = (thisscroll.yStart)*16+d;
								blah.right=(thisscroll.xEnd) * 16+d;
								blah.bottom=(thisscroll.yEnd ) *16+d;
								FrameRect(ThisBackBuffer.DC(), &blah,curbrush);
							
						}
						DeleteObject(curbrush);

				

			}

		

}
	return 0;
}//End Function


int SaveScroll(){

  FILE* fip = fopen(GBA.FileLoc,"r+b");
  fseek(fip,scrollpnt,SEEK_SET);
   fwrite(&scroll.Room,1,1,fip);
  
  fwrite(&scroll.Number,1,1,fip);
  for(int i = 0; i<scroll.Number+2;i++){
  fwrite(&scroll.Scrolls[i]. xStart,1,1,fip);
  fwrite(&scroll.Scrolls[i]. xEnd,1,1,fip);
  fwrite(&scroll.Scrolls[i]. yStart,1,1,fip);
  fwrite(&scroll.Scrolls[i]. yEnd,1,1,fip);
  }

fclose(fip);
return 0;
}//End Function


int initScroll(){//returns the address
u8 tRoom;
u32 ScrollOffset;
u32 counter;
u32 Scroll=(GameEngine->Members.gScrolls[Combos[cArea].GetListIndex()]  );
counter = 0;
GameEngine->OpenFile();
GBA.ROM=GameEngine->oFP();
start_Check:
if( GameEngine->isZM()){

fseek(GBA.ROM,Scroll - 0x8000000  + counter,SEEK_SET);
fread(&ScrollOffset,sizeof(long),1,GBA.ROM);
}else if(GameEngine->isMF()){

fseek(GBA.ROM,Scroll - 0x8000000  + counter,SEEK_SET);
fread(&ScrollOffset,sizeof(long),1,GBA.ROM);
}
//ScrollOffset = ScrollOffset


/*this too*/
fseek(GBA.ROM,ScrollOffset - 0x8000000,SEEK_SET);
tRoom = fgetc(GBA.ROM);
//start_Check:
if( tRoom != Combos[cRoom].GetListIndex()){

    /* That need to be modified*/
		if(tRoom == 0xFF){
        scrollpnt = ScrollOffset - 0x8000000;
          
                RoomScrollOffset = Scroll + counter;
           
        return RoomScrollOffset;
		}
    //ScrollOffset = ScrollOffset + 4
    counter +=  4;
    goto start_Check;
}else{
    scrollpnt = ScrollOffset - 0x8000000;
	
                RoomScrollOffset = Scroll + counter;
	
      return RoomScrollOffset;
	}//End if 

 

  
  
 return RoomScrollOffset;
}//End Function

int EditScrollData(){
	
//Find Enough Space
u32 blah=0;
int i =0;


fseek(GBA.ROM,initScroll()- 0x8000000,SEEK_SET);
scrollpnt = GBA.FindFreeSpace( scroll.Number*4 + 2 + 4,0xFF);
blah=scrollpnt + 0x8000000;
fwrite(&blah,4,1,GBA.ROM);
SaveScroll();
GBA.Reopen();
	return 0;
}//End Function

/*
int MoveObj(u8 ObjNum, u8 Where){
RECT thisrect,screenrect;
if(CurObjT == dScroll){
GetWindowRect(hScroll[ObjNum],&thisrect);
}else if(CurObjT == dDoor){
GetWindowRect(hDoors[ObjNum],&thisrect);
GetWindowRect(hwndMap,&screenrect);
}else if(CurObjT == dSprite){
GetWindowRect(hSprite[ObjNum],&thisrect);
}else{
	return 0;
}
if(CheckBoxes[sMove].value()){

switch(Where){
case 1:
	//Left
	if(((thisrect.left)/16)>=0){
		if(CurObjT == dScroll){
        scroll.Scrolls[ObjNum].xStart -=1;   //((thisrect.left-screenrect.left)/16)-1;
        scroll.Scrolls[ObjNum].xEnd -= 1;//       (thisrect.right/16-1) - scroll.Scrolls[ScrollNum].xStart ;
	    DrawScrolls();
		}else if(CurObjT == dDoor){
		Doors[DoorBuf.DoorNotes[ObjNum]].XEntrance-=1;
        Doors[DoorBuf.DoorNotes[ObjNum]].DWidth-=1;
		DisplayDoors();
		}else if(CurObjT == dSprite){
			switch(Combos[cSpriteSet].GetListIndex()){
			case 0:
				Enemy1.Enemies[ObjNum].X -=1;
				break;
			case 1:
				Enemy2.Enemies[ObjNum].X -=1;
				break;
			case 2: 
				Enemy3.Enemies[ObjNum].X -=1;
				break;

			}
			ShowSprites((u8)Combos[cSpriteSet].GetListIndex());
		}
	}
	break;
case 2:
	
			if(CurObjT == dScroll){
				scroll.Scrolls[ObjNum].xStart +=1;     
				scroll.Scrolls[ObjNum].xEnd += 1;       
				DrawScrolls();
			}else if(CurObjT == dDoor){
				Doors[DoorBuf.DoorNotes[ObjNum]].XEntrance+=1;
				Doors[DoorBuf.DoorNotes[ObjNum]].DWidth+=1;
				DisplayDoors();
			}else if(CurObjT == dSprite){
				switch(Combos[cSpriteSet].GetListIndex()){
				case 0:
					Enemy1.Enemies[ObjNum].X +=1;
				break;
				case 1:
					Enemy2.Enemies[ObjNum].X +=1;
				break;
				case 2: 
					Enemy3.Enemies[ObjNum].X +=1;
				break;
 
				}
               ShowSprites((u8)Combos[cSpriteSet].GetListIndex());
			}
		
		
	break;
case 3:
       		if(((thisrect.top)/16)>=0){
				if(CurObjT == dScroll){
	               scroll.Scrolls[ObjNum].yStart -=1;
				   scroll.Scrolls[ObjNum].yEnd -=1;    
                   DrawScrolls();
				}else if(CurObjT == dDoor){
				   Doors[DoorBuf.DoorNotes[ObjNum]].YEntrance-=1;
				   Doors[DoorBuf.DoorNotes[ObjNum]].DHeight-=1;
				   DisplayDoors();
				}else if(CurObjT == dSprite){
			switch(Combos[cSpriteSet].GetListIndex()){
			case 0:
				Enemy1.Enemies[ObjNum].Y -=1;
				break;
			case 1:
				Enemy2.Enemies[ObjNum].Y -=1;
				break;
			case 2: 
				Enemy3.Enemies[ObjNum].Y -=1;
				break;
 
			}
               ShowSprites((u8)Combos[cSpriteSet].GetListIndex());
				}
			}
			break;
case 4:

	if(((thisrect.top)/16)>=0){
		if(CurObjT == dScroll){
          scroll.Scrolls[ObjNum].yStart +=1;
          scroll.Scrolls[ObjNum].yEnd +=1;    
          DrawScrolls();
		}else if(CurObjT == dDoor){
		  Doors[DoorBuf.DoorNotes[ObjNum]].YEntrance+=1;
		  Doors[DoorBuf.DoorNotes[ObjNum]].DHeight+=1;
		  DisplayDoors();
		}else if(CurObjT == dSprite){
			switch(Combos[cSpriteSet].GetListIndex()){
			case 0:
				Enemy1.Enemies[ObjNum].Y +=1;
				break;
			case 1:
				Enemy2.Enemies[ObjNum].Y +=1;
				break;
			case 2: 
				Enemy3.Enemies[ObjNum].Y +=1;
				break;
 
			}
               ShowSprites((u8)Combos[cSpriteSet].GetListIndex());
				}
	}
	break;
}
}else if(CheckBoxes[sResize].value() == 1){
   switch(Where){
case 1:
	//Left

       if(CurObjT == dDoor){
		 
		 
		Doors[DoorBuf.DoorNotes[ObjNum]].DWidth-=1;
		if(	Doors[DoorBuf.DoorNotes[ObjNum]].DWidth<1) 	Doors[DoorBuf.DoorNotes[ObjNum]].DWidth=1;
		 DisplayDoors();
       } else if(CurObjT == dScroll){
        scroll.Scrolls[ObjNum].xEnd -= 1;
	    DrawScrolls();
	   }
   

	break;
   case 2:
	
				if(CurObjT == dScroll){
					
					scroll.Scrolls[ObjNum].xEnd += 1;   
					if(	scroll.Scrolls[ObjNum].xEnd > 0xFF) 	scroll.Scrolls[ObjNum].xEnd = 0xFF;

		     		DrawScrolls();
				}else if(CurObjT == dDoor){
				   Doors[DoorBuf.DoorNotes[ObjNum]].DWidth+=1;
				   if( Doors[DoorBuf.DoorNotes[ObjNum]].DWidth > 0xFF)  Doors[DoorBuf.DoorNotes[ObjNum]].DWidth = 0xFF;
		           DisplayDoors();
				}
	
		break;

case 3:
       		if(((thisrect.top)/16)>=0){
				if(CurObjT == dScroll){
					 scroll.Scrolls[ObjNum].yEnd -=1;    
					  
					 DrawScrolls();
				}else if(CurObjT == dDoor){
					 Doors[DoorBuf.DoorNotes[ObjNum]].DHeight-=1;
					 if( Doors[DoorBuf.DoorNotes[ObjNum]].DHeight <1)  Doors[DoorBuf.DoorNotes[ObjNum]].DHeight = 1;
				     DisplayDoors();
				}
			}

			break;
case 4:

	if(((thisrect.top)/16)>=0){
	  if(CurObjT == dScroll){
           scroll.Scrolls[ObjNum].yEnd +=1;    
           DrawScrolls();
		}else if(CurObjT == dDoor){
           Doors[DoorBuf.DoorNotes[ObjNum]].DHeight+=1;
		   DisplayDoors();
		}
	}
	break;
}
}
	
	return 0;
}*/